package batch.ecircle;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.*;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.*;
import java.io.*;
import java.util.Date;

public class makePromotionCode
{
    final String TODAY = DateFormatUtils.format(new Date(), "yyyyMMdd");

    String strCountry = "";     //Which Country
    String strSplit   = "";     //Separate file or Not
    String strFileGB  = "";

    public makePromotionCode(String strPrCountry, String strPrSplit) {
        strCountry = (strPrCountry.equals("all")) ? "" : StringUtils.defaultString(strPrCountry);
        strSplit   = StringUtils.defaultString(strPrSplit);
    }

    public static void main(String [] args) {
        Logger.getLogger("ecircle.makePromotionCode").info("( * ) Start.");

        makePromotionCode mkUser = null;

        if( args.length == 2 ){
            mkUser = new makePromotionCode(args[0], args[1]);
        }
        else{
            System.exit(-1);
        }

        mkUser.startMake();

        Logger.getLogger("ecircle.makePromotionCode").info("( * ) End.");
    }

    //Make a log file and a data file.
    public void startMake(){
        Connection   conn = null;
        Statement    stmt = null;
        ResultSet    rs   = null;
        OutputStreamWriter oswFile = null;

        String fileNm  = "/home01/sfc/batch/ecircle/data/Promotion_Code_" + strCountry + "_" + TODAY + ".txt";

        int cnt = 0;
        int rsCnt = 0;

        StringBuffer sbTemp = new StringBuffer();

        StringBuffer sQuery = new StringBuffer()
                             .append("SELECT B.MST_CD                          \n")
                             .append("     , B.TITLE_NM                        \n")
                             .append("  FROM G_PMGM_MST_SITE  A                \n")
                             .append("     , G_PMGM_MST       B                \n")
                             .append(" WHERE A.MST_CD  = B.MST_CD              \n")
                             .append("   AND A.SITE_CD = '" + strCountry + "'  \n");

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@eu-anydb:1521:ORAEU", "gsms", "any02gs");
            //conn = DriverManager.getConnection("jdbc:oracle:thin:@eu-stgdb:1521:ORAEU", "gsms", "any02gs");
            conn.setAutoCommit(true);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sQuery.toString());
			rsCnt = rs.getMetaData().getColumnCount();

            oswFile = new OutputStreamWriter(new FileOutputStream(fileNm, false), "ISO-8859-1");

            while(rs.next()){
				sbTemp.setLength(0);

                for(int i=1;i<=rsCnt;i++) {
                    sbTemp.append(ObjectUtils.toString(rs.getObject(i), ""));

                    if( i == rsCnt ){
                        sbTemp.append("\n");
                    }
                    else{
                        sbTemp.append("|");
                    }
                }

                oswFile.write(sbTemp.toString());
                cnt++;

                if(cnt % 500 == 0){
                    Logger.getLogger("ecircle.makePromotionCode").info("here we are == " + cnt);
                }
            }

            Logger.getLogger("ecircle.makePromotionCode").info("Total Count : " + cnt);
        }
        catch(Exception ex) {
            ex.printStackTrace();

            Logger.getLogger("ecircle.makePromotionCode").info(ex.toString());
            StackTraceElement[] stElement = ex.getStackTrace();
            for(int p=0; p<stElement.length;p++){
                Logger.getLogger("ecircle.makePromotionCode").info(stElement[p].toString());
            }
        }
        finally {
            if (rs   != null) try { rs.close(); }   catch (Exception e) { e.printStackTrace(); }
            if (stmt != null) try { stmt.close(); } catch (Exception e) { e.printStackTrace(); }
            if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
            if (oswFile != null) try { oswFile.close(); } catch (Exception e) { e.printStackTrace(); }
        }
    }
}
